<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请题目类型" clearable size="small">
          <el-option
            v-for="dict in typeList"
            :key="dict.id"
            :label="dict.name"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dt:subject:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dt:subject:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dt:subject:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dt:subject:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="subjectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id" width="100"/>
      <el-table-column label="名称" align="center" prop="name" width="500"/>
      <el-table-column label="状态" align="center" prop="state" width="100" :formatter="stateFormat"/>
      <el-table-column label="类型" align="center" prop="typeName"/>
      <el-table-column label="客户端" align="center" prop="clientId"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dt:subject:query']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dt:subject:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改题目对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      custom-class="dialogClass"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="20" :offset="2">
            <el-form-item label="题目：" prop="name" label-width="100px">
              <div class="el-input el-input--medium is-disabled">
                {{form.name}}
              </div>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="10" :offset="2">
            <el-form-item label="科目类型：" prop="name" label-width="100px">
              <div class="el-input el-input--medium is-disabled">
                {{form.typeName}}
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="客户端：" prop="name" label-width="100px">
              <div class="el-input el-input--medium is-disabled">
                {{form.clientId}}
              </div>
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="2" :offset="2">
            <el-form-item label="题目选项：" prop="name" label-width="100px">

            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-table
              :data="form.items"
              border
              style="width: 100%">
              <el-table-column
                prop="answerNumber"
                label="编码"
                width="100"
                align="center">
              </el-table-column>
              <el-table-column
                prop="name"
                label="名称"
                align="center">
              </el-table-column>
              <el-table-column
                prop="isAnswer"
                label="是否答案"
                width="100"
                align="center"
                :formatter="answerFormat">
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="20" :offset="2">
            <el-form-item label="状态：" prop="state" label-width="100px">
              <div class="el-input el-input--medium is-disabled">
                {{stateFormat(form)}}
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

    </el-dialog>
  </div>
</template>

<script>
import {listSubject, getSubject, delSubject, addSubject, updateSubject, exportSubject} from "@/api/dt/subject";
import {listType} from "@/api/dt/type"

export default {
  name: "Subject",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 题目表格数据
      subjectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      stateOptions: [],
      // 是否答案选项
      answerOptions: [{
        dictValue: 0,
        dictLabel: "否"
      },{
        dictValue: 1,
        dictLabel: "是"
      }],
      // 题库类型
      typeList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        state: undefined,
        typeId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

    };
  },
  created() {
    this.getList();
    this.getDicts("dt_subject_status").then(response => {
      this.stateOptions = response.data;
    });
  },
  methods: {
    /** 查询题目列表 */
    getList() {
      this.loading = true;
      listSubject(this.queryParams).then(response => {
        this.subjectList = response.rows;
        this.total = response.total;
      });
      listType().then(response => {
        this.typeList = response.rows;
      })
      this.loading = false;
    },
    // 状态字典翻译
    stateFormat(row, column) {
      return this.selectDictLabel(this.stateOptions, row.state);
    },
    answerFormat(row,column) {
      return this.selectDictLabel(this.answerOptions, row.isAnswer);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateDate: undefined,
        state: undefined,
        stateName: undefined,
        delFlag: undefined,
        answerId: undefined,
        typeId: undefined,
        answerType: undefined,
        isExam: undefined,
        jx: undefined,
        clientId: undefined,
        typeName: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加题目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSubject(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "题目详情";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSubject(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addSubject(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除题目编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delSubject(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(function () {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有题目数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportSubject(queryParams);
      }).then(response => {
        this.download(response.msg);
      }).catch(function () {
      });
    }
  }
};
</script>
<style>
.dialogClass {
  top: 10vh;
}
</style>
